<script lang="ts" setup name="Notice">
import { QForm, QTable, type FormItemRows, type TableColums } from '@meetjs/element-plus-kit'
import { delNotice, listNotice } from '@/api/system/notice/index'
import { warnBox } from '@/utils/utils'
import { checkPermission } from '@/utils/permission'
import { getDicts } from '@/api/system/dict/index'
import type { IGetDictsRes } from '@/api/system/dict/type'
import EditDialog from './components/EditDialog.vue'
import type { NoticeList, QueryParams } from '@/api/system/notice/type'

// form
const formValue = ref<QueryParams>({
	noticeTitle: '',
	createBy: '',
	noticeType: ''
})

const sysNoticeType = ref<IGetDictsRes>([])
const sysNoticeStatus = ref<IGetDictsRes>([])

const formOptions = ref<FormItemRows[]>([
	{
		type: 'input',
		label: '公告标题',
		prop: 'noticeTitle'
	},
	{
		type: 'input',
		label: '操作人员',
		prop: 'createBy'
	},
	{
		type: 'select',
		label: '公告类型',
		prop: 'noticeType',
		options: sysNoticeType as unknown as any,
		attrs: {
			style: {
				width: '200px'
			}
		}
	},
	{
		type: 'select',
		label: '状态',
		prop: 'status',
		options: sysNoticeStatus as unknown as any,
		attrs: {
			style: {
				width: '200px'
			}
		}
	}
])

// table
const loading = ref(false)
const page = ref({
	currentPage: 1,
	pageSize: 10,
	total: 0
})
const tableData = ref<NoticeList[]>([])
const columns = ref<TableColums[]>([
	{
		type: 'index',
		label: '序号',
		align: 'center',
		width: 60
	},
	{
		label: '公告标题',
		prop: 'noticeTitle'
	},
	{
		label: '公告类型',
		prop: 'noticeType'
	},
	{
		label: '状态',
		prop: 'status'
	},
	{
		label: '创建者',
		prop: 'createBy'
	},
	{
		label: '创建时间',
		prop: 'createTime'
	},
	{
		prop: 'action',
		fixed: 'right',
		label: '操作',
		width: 160,
		align: 'center'
	}
])

// dialog
const editDialogRef = useTemplateRef('editDialogRef')

const getTableData = async () => {
	loading.value = true
	try {
		const res = await listNotice({
			pageNum: page.value.currentPage,
			pageSize: page.value.pageSize,
			...formValue.value
		})
		tableData.value = res.data.rows || []
		page.value.total = res.data.total || 0
	} catch (error) {
		console.error('获取数据失败:', error)
	} finally {
		loading.value = false
	}
}

const handleAdd = () => {
	editDialogRef.value?.init('add')
}

const handleEdit = (row: NoticeList) => {
	editDialogRef.value?.init('edit', row)
}

const handleDelete = async (rowId: string) => {
	await warnBox({ title: '提示' })
	await delNotice(rowId)
	ElMessage.success('删除成功')
	getTableData()
}

const init = () => {
	getDicts('sys_sys_notice_type').then(data => {
		sysNoticeType.value = data
	})
	getDicts('sys_sys_notice_status').then(data => {
		sysNoticeStatus.value = data
	})
}

onMounted(() => {
	init()
	getTableData()
})
</script>

<template>
	<div class="table-page-box">
		<div class="filter-box">
			<QForm
				inline
				:model="formValue"
				:form-options="formOptions"
				:buttons="['search', 'reset']"
				@search="getTableData"
				@reset="getTableData"
			/>
		</div>
		<div class="table-box">
			<QTable
				:data="tableData"
				:columns="columns"
				:page="page"
				:loading="loading"
				style="width: 100%"
				@pageChange="getTableData"
			>
				<template #headerLeft>
					<el-button
						v-if="checkPermission(['system:notice:add'])"
						type="primary"
						@click="handleAdd"
					>
						新增
					</el-button>
				</template>
				<template #status="{ row }">
					<DictTag
						:options="sysNoticeStatus"
						:value="row.status"
					/>
				</template>
				<template #noticeType="{ row }">
					<DictTag
						:options="sysNoticeType"
						:value="row.noticeType"
					/>
				</template>
				<template #action="{ row }">
					<el-button
						v-if="checkPermission(['system:notice:edit'])"
						type="primary"
						link
						@click="handleEdit(row)"
					>
						修改
					</el-button>
					<el-button
						v-if="checkPermission(['system:notice:remove'])"
						type="danger"
						link
						@click="handleDelete(row.id)"
					>
						删除
					</el-button>
				</template>
			</QTable>
		</div>
		<EditDialog
			ref="editDialogRef"
			@refresh="getTableData"
		/>
	</div>
</template>
